package com.ruoyi.edit.controller;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.edit.domain.VO.CoursePackageVO;
import com.ruoyi.edit.service.ICourseContentService;
import com.ruoyi.edit.service.IVendorCourseService;
import com.ruoyi.edit.service.OssService;
import com.ruoyi.edit.utils.AssertUtil;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.edit.domain.CoursePackage;
import com.ruoyi.edit.service.ICoursePackageService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 课程包Controller
 * 
 * @author qiufeng
 * @date 2022-03-10
 */
@Controller
@RequestMapping("/system/package")
public class CoursePackageController extends BaseController
{
    private String prefix = "system/package";

    @Autowired
    private ICoursePackageService coursePackageService;

    @GetMapping()
    public String packageView()
    {
        return prefix + "/packages";
    }

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ICourseContentService courseContentService;

    @Autowired
    private IVendorCourseService vendorCourseService;

    @Autowired
    private OssService ossService;

    @Value("${oss.path}")
    public  String path;

    @Value("${oss.url}")
    public  String ossUrl;
    /**
     * 查询课程包列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CoursePackage coursePackage)
    {
        startPage();
        List<CoursePackage> list = coursePackageService.selectCoursePackageList(coursePackage);
        return getDataTable(list);
    }

    /**
     * 导出课程包列表
     */
    @Log(title = "课程包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CoursePackage coursePackage)
    {
        List<CoursePackage> list = coursePackageService.selectCoursePackageList(coursePackage);
        ExcelUtil<CoursePackage> util = new ExcelUtil<CoursePackage>(CoursePackage.class);
        return util.exportExcel(list, "课程包数据");
    }

    /**
     * 新增课程包
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存课程包
     */
    @Log(title = "课程包", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CoursePackage coursePackage)
    {
        if (AssertUtil.isEmpty(coursePackage.getOrderNum())){
            coursePackage.setOrderNum(0);
        }
        coursePackageService.insertCoursePackage(coursePackage);
        return toAjax(1);
    }

    /**
     * 修改课程包
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        List<SysDictData> list = dictTypeService.selectDictDataByType("course_package_type");
        CoursePackage coursePackage = coursePackageService.selectCoursePackageById(id);
        if (AssertUtil.isNotEmpty(coursePackage.getTypeName())){
           String[]  typeName = Convert.toStrArray(coursePackage.getTypeName());
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setIsDefault("N");
                for (int j = 0; j < typeName.length; j++) {
                    if (StringUtils.equals(list.get(i).getDictValue() , typeName[j])){
                        list.get(i).setIsDefault("Y");
                    }
                }
            }
        }

        mmap.put("coursePackage", coursePackage);
        mmap.put("dictDataList", list);
        return prefix + "/edit";
    }




    /**
     * 修改课程包
     */
    @GetMapping("/editWeek/{id}")
    public String editWeek(@PathVariable("id") String id, ModelMap mmap)
    {
        CoursePackage coursePackage = coursePackageService.selectCoursePackageById(id);
        mmap.put("coursePackage", coursePackage);
        return prefix + "/editParent";
    }
    /**
     * 修改保存课程包
     */
    @Log(title = "课程包", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CoursePackage coursePackage)
    {

        return toAjax(coursePackageService.updateCoursePackage(coursePackage));
    }

    /**
     * 删除课程包
     */
    @Log(title = "课程包", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(coursePackageService.deleteCoursePackageByIds(ids));
    }



    /**
     * 上传图片
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file,String id)
    {
        String originalName = file.getOriginalFilename();
        String extension = originalName.substring(originalName.lastIndexOf("."));
        String fileName = "admin" + "_"+ System.currentTimeMillis() + extension;
        String url = ossService.aliyunOssUpload(path+ "common/"+ fileName,file);
        CoursePackage coursePackage = coursePackageService.selectCoursePackageById(id);
        coursePackage.setCover(ossUrl+url);
        coursePackageService.updateCoursePackage(coursePackage);
        return toAjax(1);
    }

    /**
     * 新增课程包子节点
     */
    @GetMapping("/addParent")
    public String addParent(String id, ModelMap mmap)
    {
        mmap.put("id", id);
        return prefix + "/addParent";
    }

    /**
     * 课程包周
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/parentPackages")
    public String parentPackages(String id, ModelMap mmap)
    {
        mmap.put("id", id);
        return prefix + "/parentPackages";
    }


    /**
     * 课程包周
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/copyWeekPackages")
    public String copyWeekPackagesHtml(String id, ModelMap mmap)
    {
        mmap.put("id", id);
        return prefix + "/copyWeekPackages";
    }



    @PostMapping("/copyWeekPackages")
    @ResponseBody
    public AjaxResult copyWeekPackages(String id,String packagesId)
    {
        courseContentService.copyWeekPackages(id,packagesId);
        return toAjax(1);
    }



    /**
     * 课程包周
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/copyWeekCoursePackages")
    public String copyWeekCoursePackagesHtml(String id, ModelMap mmap)
    {
        mmap.put("id", id);
        return prefix + "/copyWeekCoursePackages";
    }



    @PostMapping("/copyWeekCoursePackages")
    @ResponseBody
    public AjaxResult copyWeekCoursePackages(String id,String packagesId)
    {
        courseContentService.copyWeekCoursePackages(id,packagesId);
        return toAjax(1);
    }

    @PostMapping("/courseNameList")
    @ResponseBody
    public TableDataInfo list(String  name)
    {
        startPage();
        List<CoursePackageVO> list = coursePackageService.selectCourseNameList(name);
        return getDataTable(list);
    }

}
